﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Text;


namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class RoleSetController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.RoleSet BllRoleSet = new BLL.RoleSet();
        [HttpPost]
        [Route("api/RoleSet/Add")]
        public IHttpActionResult AddRoleSet(Model.RoleSet model)
        {
            log.Info($"RoleID:{model.RoleID};ModID:{model.ModID}");

            ResultMsg resultMsg = new ResultMsg();
            //验证角色ID RID是否存在  
            if (BllRoleSet.Exists(model.RoleID, model.ModID))
            {
                BllRoleSet.Delete(model.RoleID, model.ModID);
                Model.RoleSet MolRoleSet = new Model.RoleSet();
                MolRoleSet.RoleID = model.RoleID;
                MolRoleSet.ModID = model.ModID;
                bool flag = BllRoleSet.Add(MolRoleSet);
                if (flag)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = flag;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "";
                }
            }
            else
            {
                Model.RoleSet MolRoleSet = new Model.RoleSet();
                MolRoleSet.RoleID = model.RoleID;
                MolRoleSet.ModID = model.ModID;
                bool flag = BllRoleSet.Add(MolRoleSet);
                if (flag)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = flag;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "";
                }
            }

            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/RoleSet/AddList")]
        public IHttpActionResult AddRoleSetList(VM_RoleSet modelList)
        {

            ResultMsg resultMsg = new ResultMsg();
            for (int i = 0; i < modelList.RoleSetList.Count; i++)
            {
                Model.RoleSet model = modelList.RoleSetList[i];

                //验证角色ID RID是否存在  
                if (BllRoleSet.Exists(model.RoleID, model.ModID))
                {
                    BllRoleSet.Delete(model.RoleID, model.ModID);
                    Model.RoleSet MolRoleSet = new Model.RoleSet();
                    MolRoleSet.RoleID = model.RoleID;
                    MolRoleSet.ModID = model.ModID;
                    bool flag = BllRoleSet.Add(MolRoleSet);
                    if (flag)
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = flag;
                    }
                    else
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "";
                    }
                }
                else
                {
                    Model.RoleSet MolRoleSet = new Model.RoleSet();
                    MolRoleSet.RoleID = model.RoleID;
                    MolRoleSet.ModID = model.ModID;
                    bool flag = BllRoleSet.Add(MolRoleSet);
                    if (flag)
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = flag;
                    }
                    else
                    {
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "";
                    }
                }
            }


            return Json(resultMsg);
        }
        /// <summary>
        /// 获取角色拥有权限列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns> 
        [HttpPost]
        [Route("api/GetRoleSetList")]
        public IHttpActionResult GetRoleSetList(Model.RoleSet model)
        {
            log.Info($"获取角色拥有权限列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = BllRoleSet.GetList(" RoleID = " + model.RoleID).Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到角色对应权限信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/RoleSet/Del")]
        public IHttpActionResult DelRoleSet(Model.RoleSet model)
        {
            log.Info($"删除角色拥有权限信息");
            ResultMsg resultMsg = new ResultMsg();

            if (BllRoleSet.Delete(model.RoleID, model.ModID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/RoleSet/GetModule")]
        public IHttpActionResult GetModule(Model.Role role)
        {
            int RoleID = role.RoleID;

            BLL.RoleSet rp = new BLL.RoleSet();
            string strwhere = "RoleID = " + RoleID;
            DataTable dt = rp.GetList(strwhere).Tables[0];

            ResultMsg resultMsg = new ResultMsg();

            if (dt.Rows.Count > 0)
            {
                BLL.Module mod = new BLL.Module();
                strwhere = "modid in ( " + dt.Rows[0]["ModID"].ToString() + " )";
                dt = mod.GetList(strwhere).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Count = dt.Rows.Count;
                    resultMsg.Data = dt;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "";
                }
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "";
            }


            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/RoleSet/GetModuleID")]
        public IHttpActionResult GetModuleID(Model.Role role)
        {
            int RoleID = role.RoleID;

            BLL.RoleSet rp = new BLL.RoleSet();
            string strwhere = "RoleID = " + RoleID+ " and b.ModLevel>1 ";
            DataTable dt = rp.GetList(0,strwhere, " a.ModID ").Tables[0];
            ResultMsg resultMsg = new ResultMsg();
            if (dt.Rows.Count > 0)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Count = dt.Rows.Count;
                resultMsg.Data = dt;

            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Count = 0;
                resultMsg.Data = "";
            }


            return Json(resultMsg);
        }

        //[HttpPost]
        //[Route("api/RoleSet/GetModuleByRoleID")]
        //public IHttpActionResult GetModuleByRoleID(Model.Role role)
        //{
        //    int RoleID = role.RoleID;

        //    BLL.RoleSet rp = new BLL.RoleSet();

        //    DataTable dt = rp.getModuleByRoleID(role.RoleID.ToString()).Tables[0];

        //    ResultMsg resultMsg = new ResultMsg();

        //    if (dt.Rows.Count > 0)
        //    {

        //        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
        //        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
        //        resultMsg.Count = dt.Rows.Count;
        //        resultMsg.Data = dt;

        //    }
        //    else
        //    {
        //        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
        //        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
        //        resultMsg.Data = "";
        //    }


        //    return Json(resultMsg);
        //}


        [HttpPost]
        [Route("api/RoleSet/FindModIDByRoleID")]
        public IHttpActionResult FindModIDByRoleID(string RoleID)
        {

            BLL.RoleSet rp = new BLL.RoleSet();

            DataTable dt = rp.GetList("RoleID="+RoleID).Tables[0];

            ResultMsg resultMsg = new ResultMsg();

            if (dt.Rows.Count > 0)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Count = dt.Rows.Count;
                resultMsg.Data = dt;

            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "";
            }


            return Json(resultMsg);
        }


        [HttpPost]
        [Route("api/RoleSet/FindRoleModule")]
        public IHttpActionResult FindRoleModule(string RoleID)
        {
            BLL.RoleSet rp = new BLL.RoleSet();
            string strwhere = "RoleID = " + RoleID;
            DataTable dt = rp.GetRoleModule(RoleID.ToString()).Tables[0];

            ResultMsg resultMsg = new ResultMsg();

            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
            resultMsg.Count = dt.Rows.Count;
            resultMsg.Data = dt;


            return Json(resultMsg);
        }

    }
}
